package cn.sf.sufingadminconsumer.controller;

import cn.sf.pojo.User;
import cn.sf.sufingadminconsumer.service.UserManagerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;


@RestController
@CrossOrigin("*")

public class UserManagerController {

  @Resource private UserManagerService userManagerService;

  @RequestMapping("/selectAllUserInfo")
  @ApiOperation("分页查询维修站点")
  public PageInfo<User> selectAllUserInfo(
      @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum,
      @RequestParam(defaultValue = "2", value = "pageSize") Integer pageSize,
      Model model) {
    System.out.println("传过来的页码" + pageNum);
    // 页码容量
    PageHelper.startPage(pageNum, pageSize);
    List<User> userList = userManagerService.selectAllUserInfo();
    PageInfo<User> pageInfo = new PageInfo<>(userList);
    model.addAttribute("pageInfo", pageInfo);
    System.out.println(pageInfo.getSize());
    System.out.println(pageInfo.getPageNum());
    System.out.println(pageInfo.getPages());
    return pageInfo;
  }

  @RequestMapping("/selectById")
  public List<User> selectById(long userId) {
    System.out.println("id1");
    return userManagerService.selectById(userId);
  }

  @RequestMapping("/selectByUserName")
  public List<User> selectByUserName(String userName) {

    System.out.println("用户名查询1");
    return userManagerService.selectByUserName(userName);
  }

  @RequestMapping("/selectByEmail")
  public List<User> selectByEmail(String userEmail) {
    return userManagerService.selectByEmail(userEmail);
  }

  @RequestMapping("/selectByPhone")
  public List<User> selectByPhone(long userPhone) {
    return userManagerService.selectByPhone(userPhone);
  }

  @RequestMapping("/selectBySex")
  public List<User> selectBySex(String userSex) {
    return userManagerService.selectBySex(userSex);
  }

  @RequestMapping("/insertUserInfo")
  public String insertUserInfo(User user) {
    int result = userManagerService.insertUserInfo(user);
    if (result > 0) {
      return "success";
    } else {
      return "false";
    }
  }

  @RequestMapping("/UpdateUserInfo")
  public String UpdateUserInfo(User user) {
    int result = userManagerService.UpdateUserInfo(user);
    if (result > 0) {
      return "success";
    } else {
      return "false";
    }
  }

  @RequestMapping("/deleteById")
  public String deleteById(long userId) {
    int result = userManagerService.deleteById(userId);
    if (result > 0) {
      return "success";
    } else {
      return "false";
    }
  }
}
